<script setup>
import { queryPreregisterMessage, updatePreregister } from '@/api/Base/preregister';
import { queryPreregisterInfo } from '@/api/Base/product';
import { MinusCircleFilled } from '@vicons/antd'
import { showNotify, showConfirmDialog } from 'vant'
definePage({
    name: 'updatearrive',
    meta: {
        level: 2,
        title: '更新到货',
    },
})
const route = useRoute()

const updatearrive = reactive({
    Product: {
        remark:"",
        sPreregisterNo: "",
        photoList: [],
        serialList: [],
        faultMessage: "",
        productName: "",
        quantity: 0,
        brand: "",
        companyName: ""
    },
    filter: {
        type: "",
        showType: false,
        model: {
            SelectCustomer: {
                customer: {},
                show: false,
            },
            SelectBusinessStatus: {
                show: false,
                businessStatus: 0,
                columns: [
                    { text: '正常', value: 1 },
                    { text: '保修', value: 2 },
                    { text: '走账', value: 3 },
                ],
            },
            SelectProcurement: {
                product: {},
                Procurement: {},
                show: false,
                productShow:false
            },
            SelectPCB: {
                PCB: {},
                show: false,
            },
            SelectProduct: {
                product: {},
                show: false,
            },
            SelectFix: {
                Fix: {},
                show: false
            }
        }
    },
    data: {},
    reload: () => {
        queryPreregisterInfo({
            "preNumber": route.query.id
        }).then(res => {
            if (res.code == 0) {
                //通用属性处理
                updatearrive.Product.photoList = JSON.parse(res.data.sPhotoUrl)
                updatearrive.Product.quantity = res.data.nPreregisterNum
                updatearrive.filter.type = getLabelByValue(res.data.nType)
                updatearrive.Product.sPreregisterNo = res.data.sPreregisterNo
                switch (getLabelByValue(res.data.nType)) {
                    case "客户退换":
                        break
                    case "客户送修":
                        updatearrive.filter.model.SelectCustomer.customer.nCustomerId = res.data.nCustomerId
                        updatearrive.filter.model.SelectBusinessStatus.businessStatus = res.data.nBusinessStatus
                        updatearrive.filter.model.SelectProcurement.Procurement = {
                            sPurchaseNumber: res.data.sTypeOrderNo,
                            sRemark: res.data.sCustomerRemark,
                            productShow: false,
                            product: {
                                sProductNo: 0
                            },
                            falutMessage: "",
                            ...res.data
                        }
                        break
                    case "客供料":
                        updatearrive.filter.model.SelectProcurement.product.sProductNo = res.data.sProductNo
                        updatearrive.filter.model.SelectCustomer.customer.nCustomerId = res.data.nCustomerId
                        updatearrive.filter.model.SelectProcurement.Procurement = {
                            sPurchaseNumber: res.data.sTypeOrderNo,
                            sRemark: res.data.sCustomerRemark,
                            productShow: false,
                            product: {
                                sProductNo: 0
                            },
                            falutMessage: "",
                            ...res.data
                        }
                    case "采购物料":
                        
                        updatearrive.filter.model.SelectProcurement.Procurement = {
                            sPurchaseNumber: res.data.sTypeOrderNo,
                            sRemark: res.data.sCustomerRemark,
                            productShow: false,
                            product: {
                                sProductNo: 0
                            },
                            falutMessage: "",
                            ...res.data
                        }
                        break
                    case "外修送回":
                        break
                    case "代工交货":
                        break
                    case "PCB":
                        break
                    case "其他":
                        break
                    default:
                        break
                }
            }
        })
    }
})
onMounted(() => {
    updatearrive.reload()
})

function getLabelByValue(value) {
    // 使用switch语句来匹配值并返回相应的标签
    switch (value) {
        case 0:
            return '客户退换';
        case 1:
            return '客户送修';
        case 2:
            return '客供料';
        case 3:
            return '采购物料';
        case 4:
            return '外修送回';
        case 5:
            return '代工交货';
        case 7:
            return 'PCB';
        case 6:
            return '其他';
        default:
            return '未知'; // 或者根据需要返回其他默认值
    }
}
function getValueByLabel(label) {
    const labelToValueMap = {
        '客户退换': 0,
        '客户送修': 1,
        '客供料': 2,
        '采购物料': 3,
        '外修送回': 4,
        '代工交货': 5,
        'PCB': 7,
        '其他': 6,
    };

    // 返回对应的值，如果标签不存在则返回 undefined 或默认值
    return labelToValueMap[label] ?? '未知';
}
const columns = [
    { text: '采购物料', value: 0 },
    { text: '客户送修', value: 1 },
    { text: 'PCB订单', value: 6 },
    { text: '客户退换', value: 2 },
    { text: '外修返回', value: 3 },
    { text: '代工交货', value: 4 },
    { text: '客供料', value: 5 },
    { text: '其他', value: 7 },
];
const router = useRouter()
function onBack() {
    if (window.history.state.back)
        history.back()
    else
        router.replace('/')
}

const sn = ref("")
function addSN() {
    updatearrive.Product.serialList.push(sn.value)
    sn.value = ""
}

function updateProduct() {
    switch (updatearrive.filter.type) {
        case '客户退换':

            return;
        case '客户送修':
            updatePreregister({
                sPreregisterNo: updatearrive.Product.sPreregisterNo,
                sPhotoUrl: JSON.stringify(updatearrive.Product.photoList),
                nPreregisterNum: updatearrive.Product.quantity,
                nType: getValueByLabel(updatearrive.filter.type),
                // serialNumber: ProductInputObj.Product.serialList,
                sProductName: updatearrive.Product.productName,
                nCustomerId: updatearrive.filter.model.SelectCustomer.customer.nCustomerId,
                sReciveRemark:updatearrive.Product.remark

            }).then(res => {
                if (res.code == 0) {
                    showNotify({ type: 'success', message: res.message })
                    onBack()
                } else {
                    showNotify({ type: 'danger', message: res.message })
                }
            })
            return;
        case '采购物料':
            updatePreregister({
                sProductNo: updatearrive.filter.model.SelectProcurement.product.sProductNo,
                sPreregisterNo: route.query.id,
                sPhotoUrl: JSON.stringify(updatearrive.Product.photoList),
                nPreregisterNum: updatearrive.Product.quantity,
                nType: getValueByLabel(updatearrive.filter.type),
                sTypeOrderNo: updatearrive.filter.model.SelectProcurement.Procurement.sPurchaseNumber,
                sProductName: updatearrive.Product.productName,
                sReciveRemark:updatearrive.Product.remark
            }).then(res => {
                if (res.code == 0) {
                    console.log(res);
                    showNotify({ type: 'success', message: res.message })
                    onBack()
                } else {
                    showNotify({ type: 'danger', message: res.message })
                }
            })
        case '客供料':
            updatePreregister({
                sProductNo: updatearrive.filter.model.SelectProcurement.product.sProductNo,
                nCustomerId: updatearrive.filter.model.SelectCustomer.customer.nCustomerId,
                sPreregisterNo: route.query.id,
                sPhotoUrl: JSON.stringify(updatearrive.Product.photoList),
                nPreregisterNum: updatearrive.Product.quantity,
                nType: getValueByLabel(updatearrive.filter.type),
                sProductName: updatearrive.Product.productName,
            }).then(res => {
                if (res.code == 0) {
                    console.log(res);
                    showNotify({ type: 'success', message: res.message })
                    onBack()
                } else {
                    showNotify({ type: 'danger', message: res.message })
                }
            })
        case 4:
            return '外修送回';
        case 5:
            return '代工交货';
        case 7:
            return 'PCB';
        case 6:
            return '其他';
        default:
            return '未知'; // 或者根据需要返回其他默认值
    }
}
</script>

<template>
    <div class="py-70 px-20 flex flex-col gap-10">
        <div name="图片" class="flex flex-col w-[100%] h-[100%] bg-white relative p-10">
            <a class="text-12">登记图片</a>
            <EImageList v-model:photoList="updatearrive.Product.photoList" />
            <van-stepper class="absolute bottom-0 right-10px" v-model="updatearrive.Product.quantity" />
        </div>

        <div name="分类">
            <van-field class="rounded-[10px] h-[60px] flex items-center" v-model="updatearrive.filter.type" is-link
                readonly label="来货分类" placeholder="选择分类" @click="updatearrive.filter.showType = true" />
            <van-popup teleport="#app" v-model:show="updatearrive.filter.showType" round position="bottom">
                <van-picker :columns="columns" @cancel="updatearrive.filter.showType = false" @confirm="({ selectedOptions }) => {
                    updatearrive.filter.showType = false;
                    updatearrive.filter.type = selectedOptions[0].text;
                }" />
            </van-popup>
        </div>

        <div name="客户送修" v-if="updatearrive.filter.type == '客户送修'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectCustomer.customer.companySName" is-link readonly label="绑定客户"
                placeholder="客户" @click="updatearrive.filter.model.SelectCustomer.show = true" />
            <SelectCustomer v-model:customer="updatearrive.filter.model.SelectCustomer.customer"
                v-model:show="updatearrive.filter.model.SelectCustomer.show" />
            <van-field class="rounded-[10px] h-[60px] flex items-center" v-model="updatearrive.Product.productName"
                label="设备名称" placeholder="请输入" />
            <van-field class="rounded-[10px] h-[60px] flex items-center" v-model="updatearrive.Product.brand"
                label="品牌名称" placeholder="请输入" />
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectBusinessStatus.businessStatus" is-link readonly label="业务状态"
                placeholder="请输入" @click="updatearrive.filter.model.SelectBusinessStatus.show = true" />
            <van-popup v-model:show="updatearrive.filter.model.SelectBusinessStatus.show" round position="bottom">
                <van-picker :columns="updatearrive.filter.model.SelectBusinessStatus.columns"
                    @cancel="updatearrive.filter.model.SelectBusinessStatus.show = false" @confirm="({ selectedOptions }) => {
                        updatearrive.filter.model.SelectBusinessStatus.show = false
                        updatearrive.filter.model.SelectBusinessStatus.businessStatus = selectedOptions[0].text
                    }" />
            </van-popup>

            <div class="bg-white p-[10px] min-h-[150px] rounded-[10px]">
                <van-field class="rounded-[10px]" @blur="addSN" v-model="sn" label="序列号" placeholder="扫码">
                    <template #button>
                        <div>
                            <img @click="scanSerial()" block width="16" height="16" src="/input/扫码.png" />
                        </div>
                    </template>
                </van-field>
                <div class="flex flex-row h-20 items-center px-10"
                    v-for="(item, index) in updatearrive.Product.serialList" :key="index">
                    <MinusCircleFilled @click="() => {
                        updatearrive.Product.serialList.splice(index, 1)
                    }" class="w-16 h-16 c-[#ff4000]" />{{ item }}
                </div>

            </div>

            <van-field v-model="updatearrive.Product.remark" class="rounded-[10px]" rows="2" autosize
                label="客描故障/要求" type="textarea" maxlength="50" placeholder="请输入留言" show-word-limit />
        </div>

        <div name="采购物料" v-if="updatearrive.filter.type == '采购物料'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectProcurement.Procurement.sPurchaseNumber" is-link readonly
                label="绑定采购" placeholder="采购单" @click="updatearrive.filter.model.SelectProcurement.show = true" />
            <SelectProcurement v-model:show="updatearrive.filter.model.SelectProcurement.show"
                v-model:procurement="updatearrive.filter.model.SelectProcurement.Procurement" />
            <!-- {{ updatearrive.filter.model.SelectProcurement.Procurement }}
            <h1>prodect</h1>
            {{ updatearrive.Product }} -->
            <van-field label="采购备注" :model-value="updatearrive.filter.model.SelectProcurement.Procurement.sRemark"
                readonly />
            <!-- <van-field v-if="updatearrive?.filter?.model?.SelectProcurement?.Procurement?.purchase_record" label="测试方法"
                :model-value="getTestModel(updatearrive?.filter?.model?.SelectProcurement?.Procurement?.purchase_record[0]?.nTestModel || 0)"
                readonly /> -->

            <van-divider v-if="!updatearrive.filter.model.SelectProcurement.Procurement.sProductNo"
                content-position="center">分配物料号</van-divider>
            <van-field v-if="!updatearrive.filter.model.SelectProcurement.Procurement.sProductNo"
                class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectProcurement.product.sProductNo" is-link readonly label="绑定物料"
                placeholder="选择已有料号" @click="updatearrive.filter.model.SelectProcurement.productShow = true" />
            <SelectProduct v-model:product="updatearrive.filter.model.SelectProcurement.product"
                v-model:show="updatearrive.filter.model.SelectProcurement.productShow" />
            <van-divider v-if="!updatearrive.filter.model.SelectProcurement.Procurement.sProductNo" class="blue-300"
                @click="to('newProduct')" content-position="center">去新建物料号</van-divider>

            <van-field v-model="updatearrive.Product.remark" class="rounded-[10px]" rows="2" autosize
                label="备注" type="textarea" maxlength="50" placeholder="请输入备注" show-word-limit />

        </div>

        <div name="PCB订单" v-if="updatearrive.filter.type == 'PCB订单'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectPCB.PCB" is-link readonly label="绑定PCB订单" placeholder="PCB订单"
                @click="updatearrive.filter.model.SelectPCB.show = true" />
            <SelectPCB v-model:show="updatearrive.filter.model.SelectPCB.show"
                v-model:PCB="updatearrive.filter.model.SelectPCB.PCB" />
        </div>
        <div name="客户退换" v-if="updatearrive.filter.type == '客户退换'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectPCB.PCB" is-link readonly label="绑定PCB订单" placeholder="PCB订单"
                @click="updatearrive.filter.model.SelectPCB.show = true" />
            <SelectPCB v-model:show="updatearrive.filter.model.SelectPCB.show"
                v-model:PCB="updatearrive.filter.model.SelectPCB.PCB" />
        </div>
        <div name="外修返回" v-if="updatearrive.filter.type == '外修返回'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectFix.Fix" is-link readonly label="绑定维修订单" placeholder="PCB订单"
                @click="updatearrive.filter.model.SelectFix.show = true" />
            <SelectFix v-model:show="updatearrive.filter.model.SelectFix.show"
                v-model:fix="updatearrive.filter.model.SelectFix.Fix" />
        </div>
        <div name="代工交货" v-if="updatearrive.filter.type == '代工交货'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectPCB.PCB" is-link readonly label="绑定PCB订单" placeholder="PCB订单"
                @click="updatearrive.filter.model.SelectPCB.show = true" />
            <SelectPCB v-model:show="updatearrive.filter.model.SelectPCB.show"
                v-model:PCB="updatearrive.filter.model.SelectPCB.PCB" />
        </div>
        <div name="客供料" v-if="updatearrive.filter.type == '客供料'" class="gap-[10px] flex flex-col py-[20px]">
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectCustomer.customer.nCustomerId" is-link readonly label="绑定客户"
                placeholder="客户" @click="updatearrive.filter.model.SelectCustomer.show = true" />
            <SelectCustomer v-model:customer="updatearrive.filter.model.SelectCustomer.customer"
                v-model:show="updatearrive.filter.model.SelectCustomer.show" />
            <!-- <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectProduct.product.sProductName" is-link readonly label="绑定物料"
                placeholder="绑定物料" @click="updatearrive.filter.model.SelectProduct.show = true" />
            <SelectProduct v-model:product="updatearrive.filter.model.SelectProduct.product"
                v-model:show="updatearrive.filter.model.SelectProduct.show" /> -->
            <van-divider content-position="center">分配物料号</van-divider>
            <van-field class="rounded-[10px] h-[60px] flex items-center"
                v-model="updatearrive.filter.model.SelectProcurement.product.sProductNo" is-link readonly label="绑定物料"
                placeholder="选择已有料号" @click="updatearrive.filter.model.SelectProcurement.productShow = true" />
            <SelectProduct v-model:product="updatearrive.filter.model.SelectProcurement.product"
                v-model:show="updatearrive.filter.model.SelectProcurement.productShow" />
            <van-divider class="blue-300" @click="to('newProduct')" content-position="center">去新建物料号</van-divider>

            <van-field v-model="updatearrive.filter.model.faultMessage" class="rounded-[10px]" rows="2" autosize
                label="备注" type="textarea" maxlength="50" placeholder="请输入备注" show-word-limit />
        </div>
        <StepBottom>
            <template #default>
                <div>
                    <span class="step-bottom-h2" @click="onBack">取消</span>
                </div>

                <el-button class="step-bottom-button flex flex-col justify-center items-center text-white"
                    @click="updateProduct">更新</el-button>
            </template>
        </StepBottom>
    </div>
</template>

<style lang="less" scoped></style>
